# -*- coding:utf-8 -*-
"""
@Author : 审心
@Time : 2023/5/2 16:48
缓存动作
"""
from typing import Optional

from autoTask.taobao.redis_key_mange import RedisKey
from common.aop import exception_log_param
from common.redis import redis_client
from common.utils import StrUtil, ObjUtil, Base64Util, FileUtil
from common.logger.log import log_

# 获取 key 前缀
key_prefix = RedisKey.sycm.goods.market_rank.key
excel_key_prefix = RedisKey.sycm.goods.market_rank.key_excel


def build_key(category: str, data_cycle: str):
    """
    构造 key
    :param category: 品类名称
    :param data_cycle: 数据周期
    :return:
    """
    return None if StrUtil.any_blank(category, data_cycle) else key_prefix + f":{category}:{data_cycle}"


def build_excel_key(category: str, data_cycle: str):
    """
    构造 key
    :param category: 品类名称
    :param data_cycle: 数据周期
    :return:
    """
    return None if StrUtil.any_blank(category, data_cycle) else excel_key_prefix + f":{category}:{data_cycle}"


def set_cache(category: str, data_cycle: str, value: list, export: int):
    """
    设置缓存
    :param category: 品类名称
    :param data_cycle: 数据周期
    :param value: 值
    :param export: 过期时间
    :return:
    """
    if ObjUtil.is_empty(value):
        log_.warning(f"设置缓存时值为空 : 品类: {category}, 数据周期: {data_cycle}")
        return
    key = build_key(category, data_cycle)
    if key is None:
        log_.warning(f"设置缓存时构造key失败 : 品类: {category}, 数据周期: {data_cycle}")
        return
    redis_client.put(key=key, value=value, ex=export)


def get_cache(category: str, data_cycle: str) -> [None, list]:
    """
    获取缓存
    :param category: 品类名称
    :param data_cycle: 数据周期
    :return: 获取到返回 str 没获取到返回 None
    """
    key = build_key(category, data_cycle)
    if key is None:
        log_.warning(f"获取缓存时构造key失败 : 品类: {category}, 数据周期: {data_cycle}")
        return None
    result = redis_client.get(key=key, type_=list)
    if ObjUtil.is_empty(result):
        return None
    return result


def set_excel_cache(excel_path: str, category: str, data_cycle: str, export: int):
    """
    将表格转为 base64
    :param excel_path: 表格路径
    :param category: 品类
    :param data_cycle:数据周期
    :param export: 过期时间
    :return:
    """
    key = build_excel_key(category, data_cycle)
    if key is None:
        log_.warning(f"设置缓存时构造 key 失败 : 品类 : {category}, 数据周期 : {data_cycle}")
        return
    redis_client.put(key=key, value=Base64Util.file_to_base64(path=excel_path), ex=export)


@exception_log_param
def get_excel_cache(category: str, data_cycle: str) -> Optional[str]:
    key = build_excel_key(category, data_cycle)
    if key is None:
        log_.warning(f"获取缓存时构造 key 失败 : 品类 : {category}, 数据周期 : {data_cycle}")
        return
    base64 = redis_client.get(key=key, type_=str)
    if StrUtil.any_blank(base64):
        return None
    return Base64Util.base64_to_file(base64,
                                     FileUtil.get_project_root() + r"/DownloadData/sycm_table/goods/excel",
                                     f"{category}.xlsx")
